package org.carl.base.feishu.bot.service.impl;

import cn.hutool.core.util.StrUtil;
import org.carl.base.feishu.bot.bean.FeiShuBotBean;
import org.carl.base.feishu.bot.bean.FeiShubot;
import org.carl.base.feishu.bot.model.FeiShuBotRespResult;
import org.carl.base.feishu.bot.model.MsgModel;
import org.carl.base.feishu.bot.service.FeiShuBotService;
import org.carl.base.feishu.bot.util.FeiShuBotUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class FeiShuBotServiceImpl implements FeiShuBotService {

    /**
     * 机器人列表数据
     */
    @Autowired
    private FeiShubot feiShubot;

    @Override
    public FeiShuBotRespResult sendBot(String webhook, String secret, MsgModel msgModel) throws Exception {
        return FeiShuBotUtil.sendBot(webhook, secret, msgModel);
    }

    @Override
    public FeiShuBotRespResult sendBot(FeiShuBotBean robot, MsgModel msgModel) throws Exception {
        if(StrUtil.isNotBlank(robot.getSecret())){
            return sendBot(robot.getWebhook(), robot.getSecret(), msgModel);
        }
        return FeiShuBotUtil.sendKeyBot(robot.getWebhook(), msgModel.toString());
    }

    @Override
    public FeiShuBotRespResult sendBot(String robotId, MsgModel msgModel) throws Exception {
        FeiShuBotBean robot = getRobot(robotId);
        return sendBot(robot, msgModel);
    }

    /**
     * 获取机器人数据信息
     *
     * @param robotId String 机器人id
     * @return FeiShuBotBean
     */
    @Override
    public FeiShuBotBean getRobot(String robotId) {
        List<FeiShuBotBean> robot = feiShubot.getRobot();
        if (null != robot && robot.size() > 0) {
            return robot.stream().filter(r -> StrUtil.equals(r.getId(), robotId)).findFirst().orElse(null);
        }
        return null;
    }
}
